package com.lz.football.module.task.view
{
	import com.global.GlobalDef;
	import com.liangzhong.constants.TaskConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.MainEvent;
	import com.liangzhong.events.TaskEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.task.vo.TaskInfoVo;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.compete.CompeteTaskView;
	import com.lz.football.module.task.ITask;
	import com.lz.football.module.task.item.TaskItem;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 *任务 
	 * @author Administrator
	 * 
	 */	
	public class TaskView extends Sprite implements IComponent, ITask
	{
		/**
		 *第一条任务skin
		 * guide 
		 */		
		public static var firstTaskSkin :DisplayObject;
		/**
		 *国家任务面板 
		 */
		private var _nationTaskView:CompeteTaskView;
		
		public function TaskView()
		{
			super();
		}
		
		public function initUI(view:Object=null):void
		{
			addEvtListener();
			initTaskList();
			this.y = 200;
			this.x = 5;
		}
		
		public function addEvtListener():void
		{
			AppData.getInstance().taskManager.addEventListener(TaskEvent.UPDATE_TASK_LIST,onUpdateTaskList);
			
			AppData.getInstance().taskManager.addEventListener(TaskEvent.GET_TASK_LIST,onGetTaskList);
			AppData.getInstance().taskManager.addEventListener(TaskEvent.ACCEPT_NATION_TASK,onAcceptNationTaskHandler);
			AppEventDispatcher.getInstance().addEventListener(MainEvent.OPEN_NATION_TASK,onOpenNationTaskView);
		}
		
		protected function onAcceptNationTaskHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			if(_nationTaskView != null)
			{
				_nationTaskView.upDateTaskStatus();
			}
		}
		
		/**
		 *国家任务界面 
		 * @param event
		 * 
		 */
		protected function onOpenNationTaskView(event:Event):void
		{
			// TODO Auto-generated method stub
			if(_nationTaskView == null)
			{
				_nationTaskView = new CompeteTaskView();
				_nationTaskView.initUI();
//				this.dispatchEvent(new TaskEvent(TaskEvent.FINISH_NATION_TASK));
				AppData.getInstance().taskManager.addEventListener(TaskEvent.FINISH_NATION_TASK,onFinishNationHandler);
				_nationTaskView.type = 2;
			}
			_nationTaskView.updata();
			_nationTaskView.upDateTaskStatus();
			GlobalDef.GameWindowContainer.addChild(_nationTaskView);
		}
		/**
		 *完成国家任务 
		 * @param event
		 * 
		 */		
		protected function onFinishNationHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			if(_nationTaskView != null)
			{
				_nationTaskView.upDateTaskStatus();
			}
		}
		
		/**
		 *获得任务列表 
		 * @param event
		 * 
		 */
		private function onGetTaskList(event:TaskEvent):void
		{
			initTaskList();
		}
		/**
		 *初始化任务列表 
		 * 游戏进来后，需要判断是否有任务完成，有，弹出领取界面，并记录状态
		 */
		public function initTaskList():void
		{
			clearListItem();
			var arr:Array = AppData.getInstance().taskManager.acceptedList ;
			if(arr == null)return;
			var len:int = arr.length;
			var item:TaskItem;
			var vo:TaskInfoVo;
			for(var i:int = 0;i < len;i++)
			{
				item = new TaskItem();
				item.initUI();
				item.setTaskDate(arr[i]);
				vo = arr[i];
				if(vo.status ==  TaskConst.TASK_FINISH)//任务完成需要弹出完成界面
				{
					vo.isShowFinishView = true;
//					AppEventDispatcher.getInstance().dispatchEvent(new TaskEvent(TaskEvent.TASK_FINISH_TASK,{vo:vo},true));
				}
//				vo.isShowFinishView = true;
//				this.dispatchEvent(new TaskEvent(TaskEvent.TASK_FINISH_TASK,{vo:vo},true));
				//				item.setText("");
				this.addChild(item);
				item.y = (item.height )* i;
			}
			//@lake 2013-11-16
			if(this.numChildren > 0)
			{
				firstTaskSkin = this.getChildAt(0);
			}
		}
		
		
		/**
		 *更新任务列表 
		 * @param event
		 * 
		 */
		private function onUpdateTaskList(event:TaskEvent = null):void
		{
			var item:TaskItem;
			clearListItem();
				
			var arr:Array = AppData.getInstance().taskManager.acceptedList ;
			if(arr == null)return;
			var len:int = arr.length;
			
			for(var i:int = 0;i < len;i++)
			{
				item = new TaskItem();
				item.initUI();
				item.setTaskDate(arr[i]);
//				item.setText("");
				this.addChild(item);
				item.y = (item.height )* i;
			}
			//@lake 2013-11-16
			if(this.numChildren > 0)
			{
				firstTaskSkin = this.getChildAt(0);
			}
		}
		
		/**
		 *清理任务数据 
		 * 
		 */
		private function clearListItem():void
		{
			var item:TaskItem;
			while(this.numChildren)
			{
				item = this.removeChildAt(0) as TaskItem;
				item.destroyView();
				item = null;
			}
		}
		public function removeEvtListener():void
		{
			AppData.getInstance().taskManager.removeEventListener(TaskEvent.FINISH_NATION_TASK,onFinishNationHandler);
			
			AppData.getInstance().taskManager.removeEventListener(TaskEvent.UPDATE_TASK_LIST,onUpdateTaskList);
			
			AppData.getInstance().taskManager.removeEventListener(TaskEvent.GET_TASK_LIST,onGetTaskList);
			AppEventDispatcher.getInstance().removeEventListener(MainEvent.OPEN_NATION_TASK,onOpenNationTaskView);
		}
		
		public function destroyView():void
		{
		}
		/**
		 *更新捐赠欧元 
		 * 
		 */
		public function updateDonate():void
		{
			if(_nationTaskView != null)
				_nationTaskView.updata();
		}
	}
}